package org.mendoza.salud.hvt.client.local.gui.geo;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import org.jboss.errai.common.client.api.Caller;
import org.jboss.errai.common.client.api.RemoteCallback;
import org.jboss.errai.ui.client.widget.ListWidget;
import org.jboss.errai.ui.client.widget.Table;
import org.jboss.errai.ui.shared.api.annotations.DataField;
import org.jboss.errai.ui.shared.api.annotations.EventHandler;
import org.jboss.errai.ui.shared.api.annotations.Templated;
import org.mendoza.salud.hvt.client.shared.model.global.Pais;
import org.mendoza.salud.hvt.client.shared.model.services.GeoServiceRemote;
import org.mendoza.salud.hvt.client.shared.model.services.PersistenceServiceRemote;

import javax.annotation.PostConstruct;
import javax.enterprise.event.Observes;
import javax.inject.Inject;
import java.util.List;

/**
 * Created by administrador on 29/09/14.
 */
@Templated("PaisViewList.html#tablaListadoDePaises")
public class PaisViewList extends Composite {
    @Inject @DataField("paisesList")  @Table(root="tbody")
    private ListWidget<Pais, PaisListItem> paisesList;
    @Inject @DataField()
    Button botonRefrescar;

    @Inject
    Caller<GeoServiceRemote> geoServiceRemoteCaller;


    @PostConstruct
    public void init() {

        actualizar();
    }

    private void actualizar() {
        geoServiceRemoteCaller.call(new RemoteCallback<List<Pais>>() {
            @Override
            public void callback(List<Pais> response) {
                paisesList.getValue().addAll(response);
            }
        }).getAllPaises();
    }

    @EventHandler("botonRefrescar")
    public void botonRefrescarClick(ClickEvent event) {
        actualizar();
    }

    public void paisObserver(@Observes Pais pais) {
        paisesList.getValue().add(pais);
    }

    @EventHandler("paisesList")
    public void encuadrar(MouseOverEvent event) {
        paisesList.setStyleName("table-bordered");
    }
    @EventHandler("paisesList")
    public void Desencuadrar(MouseOutEvent event) {
        paisesList.removeStyleName("table-bordered");
    }
}
